package me.chenjp.tracker.config;

import lombok.extern.slf4j.Slf4j;
import me.chenjp.tracker.core.filter.JwtTokenAuthFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.DelegatingFilterProxy;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * @author: Chenjp
 * @description: 这个人很懒, 没有留下任何注释
 * @create: 2019-05-03 14:48
 */
@Slf4j
@Configuration
public class JwtConfig {

    @Bean
    public JwtTokenAuthFilter jwtTokenAuthFilter() {
        JwtTokenAuthFilter jwtTokenAuthFilter = new JwtTokenAuthFilter();
        Set<Pattern> excludeUrl = new HashSet<>();
        /*
         * 开发前期，先除掉所有接口的 token 认证
         * */
        excludeUrl.add(Pattern.compile("/*"));

        jwtTokenAuthFilter.setExcludeUrl(excludeUrl);

        return jwtTokenAuthFilter;
    }


    @Order(5)
    @Bean
    public FilterRegistrationBean delegatingFilterProxy() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(
                new DelegatingFilterProxy("jwtTokenAuthFilter")
        );

        return filterRegistrationBean;
    }
}
